package com.hm.system.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.common.annotation.AuthenticationAnnotation;
import com.hm.common.annotation.RequestPlanAnnotation;
import com.hm.common.model.ControllerResult;
import com.hm.system.mongo.domain.def.SysDictDef;
import com.hm.system.mongo.domain.system.SysDictInfo;
import com.hm.system.share.service.mongo.system.SysDictInfoService;
import com.hm.system.util.SystemConfigDef.Api.Rest;
import com.hm.system.web.su.BaseController;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author shishun.wang
 * @date 2016年6月2日 下午11:05:18
 * @version 1.0
 * @describe 数据字典
 */
@ResponseBody
@RestController
@RequestMapping(value = Rest.Sys.REST_SYS_DICT_INFO)
public class SysDictInfoController extends BaseController {

	public static final String swaggerTag = "系统数据字典";

	@Autowired
	private SysDictInfoService sysDictInfoService;
	
	@AuthenticationAnnotation
	@RequestMapping(value = "/{dictId}", method = RequestMethod.GET)
	@ApiOperation(value = "获取系统数据字典", httpMethod = "GET", notes = "获取系统数据字典通过ID", tags = swaggerTag)
	@RequestPlanAnnotation(title = "获取系统数据字典", content = "获取系统数据字典通过ID")
	public ResponseEntity<ControllerResult<SysDictInfo>> getSysDictInfo(
			@ApiParam(required = true, name = "dictId", value = "系统数据字典ID") @PathVariable String dictId) {
		try {
			return ControllerResult.success(sysDictInfoService.getSysDictInfo(dictId));
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(value = "添加系统数据字典", httpMethod = "POST", notes = "添加系统数据字典返回添加成功的数据字典ID", tags = swaggerTag)
	@RequestPlanAnnotation(title = "添加系统数据字典", content = "添加系统数据字典返回添加成功的数据字典ID")
	public ResponseEntity<ControllerResult<String>> addSysDictInfo(
			@ApiParam(required = true, name = "body", value = "系统数据字典信息") @RequestBody(required = true) SysDictInfo dictInfo) {
		try {
			String id = sysDictInfoService.addSysDictInfo(dictInfo, currentUserId());
			if (StringUtils.isEmpty(id)) {
				return ControllerResult.failed("添加失败");
			}
			return ControllerResult.success(id, "添加成功");
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{dictId}", method = RequestMethod.PUT)
	@ApiOperation(value = "修改系统数据字典", httpMethod = "PUT", notes = "修改系统数据字典返回修改成功的数据字典ID", tags = swaggerTag)
	@RequestPlanAnnotation(title = "修改系统数据字典", content = "修改系统数据字典返回修改成功的数据字典ID")
	public ResponseEntity<ControllerResult<String>> updateSysDictInfo(
			@ApiParam(required = true, name = "body", value = "系统数据字典信息") @RequestBody(required = true) SysDictInfo dictInfo,
			@ApiParam(required = true, name = "dictId", value = "系统数据字典ID") @PathVariable String dictId) {
		try {
			dictInfo.setId(dictId);
			String id = sysDictInfoService.updateSysDictInfo(dictInfo, currentUserId());
			if (StringUtils.isEmpty(id)) {
				return ControllerResult.failed("修改失败");
			}
			return ControllerResult.success(id, "修改成功");
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{dictId}", method = RequestMethod.DELETE)
	@ApiOperation(value = "删除系统数据字典", httpMethod = "DELETE", notes = "删除系统数据字典", tags = swaggerTag)
	@RequestPlanAnnotation(title = "删除系统数据字典", content = "修改系统数据字典")
	public ResponseEntity<ControllerResult<String>> deleteSysDictInfo(
			@ApiParam(required = true, name = "dictId", value = "系统数据字典ID") @PathVariable String dictId) {
		try {
			SysDictInfo SysDictInfo = sysDictInfoService.getSysDictInfo(dictId);
			if(null == SysDictInfo){
				return ControllerResult.failed("目标删除数据不存在,删除失败");
			}
			
			String id = sysDictInfoService.deleteSysDictInfo(SysDictInfo, currentUserId());;
			if (StringUtils.isEmpty(id)) {
				return ControllerResult.failed("删除失败");
			}
			return ControllerResult.success(id, "删除成功");
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}
	
	@AuthenticationAnnotation
	@RequestMapping(value = "/list/{dictDef}", method = RequestMethod.GET)
	@ApiOperation(value = "获取指定dictDef数据字典", httpMethod = "GET", notes = "获取指定dictDef数据字典", tags = swaggerTag)
	@RequestPlanAnnotation(title = "获取指定dictDef数据字典", content = "获取指定dictDef数据字典")
	public ResponseEntity<ControllerResult<List<SysDictInfo>>> loadChilds(
			@ApiParam(required = true, name = "dictDef", value = "数据字典类型[SEX,MARRY]") @PathVariable SysDictDef dictDef) {
		try {
			return ControllerResult.success(sysDictInfoService.getDictsByDictDef(dictDef));
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}
}
